/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: PayServiceImpl
 * Author:   zhouheng
 * Date:     2019/5/14 23:11
 * Description: 支付实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.star.pay.service;

import com.dubbo.user.api.constants.ResponseCodeEnum;
import com.star.pay.PayService;
import com.star.pay.bean.PayServiceRequest;
import com.star.pay.bean.PayServiceResponse;
import com.star.pay.exception.ExceptionUtil;
import com.star.pay.exception.ServiceException;
import com.star.pay.service.core.strategy.PayChannelStrateProxy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 〈一句话功能简述〉<br> 
 * 〈支付实现类〉
 *
 * @author zhouheng
 * @create 2019/5/14
 * @since 1.0.0
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {
    @Autowired
    PayChannelStrateProxy payChannelStrategyProxy;

    @Override
    public PayServiceResponse pay(PayServiceRequest payServiceRequest) {
        log.info("开始执行支付请求：payServiceRequest:"+payServiceRequest);

        PayServiceResponse payServiceResponse = new PayServiceResponse();
        try{
            // 支付落单，生成对应的qcode或支付表单
            String result = payChannelStrategyProxy.generatePrepalOrder(payServiceRequest);
            payServiceResponse.setPayString(result);
            payServiceResponse.setCode(ResponseCodeEnum.SYS_SUCCESS.getCode());
            payServiceResponse.setMsg(ResponseCodeEnum.SYS_SUCCESS.getMsg());
        }catch (Exception e){
            ServiceException serviceException = (ServiceException)ExceptionUtil.handlerException4biz(e);
            payServiceResponse.setCode(serviceException.getErrorCode());
            payServiceResponse.setMsg(serviceException.getErrorMsg());
        }finally {
            log.error("支付请求{},响应{}", payServiceRequest, payServiceResponse);
        }

        return payServiceResponse;
    }
}